package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.NightAuditListAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NightAuditModel;
import com.jiayang.hhotel.presenter.NightAuditPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 夜审页面
 */

public class NightAuditActivity extends XActivity<NightAuditPresenter> implements View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private TextView nightTime,systemTime;//当前时间，系统时间
    private ListView mListView;//夜审项目列表
    private Button mButton;//开始审核按钮
    private ImageView sign;

    private NightAuditListAdapter adapter;

    @Override
    public void initData(Bundle savedInstanceState) {

        initTitle(this,true,getString(R.string.night_audit),R.color.orange_color,null,null);

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        systemTime = (TextView) findViewById(R.id.tv_system_date);
        nightTime = (TextView) findViewById(R.id.tv_night_audit_date);
        mListView = (ListView) findViewById(R.id.lv_aduit);
        mButton= (Button) findViewById(R.id.bt_start_night_audit);
        sign = (ImageView) findViewById(R.id.iv_sign);
        CommonUtil.showProgressDialog(this,R.string.loading);
        getP().asyncListData(Constant.LAUGUAGE_FLAG_CHINESE);

        mButton.setOnClickListener(this);
    }

    public void setAdapter(NightAuditModel model){
        sign.setVisibility(View.VISIBLE);
        systemTime.setText(getString(R.string.system_date)+getString(R.string.space)+model.getPNauStepsGetDate().getHotelDate());
        nightTime.setText(getString(R.string.night_audit_date)+getString(R.string.space)+model.getPNauStepsGetDate().getNauditDate());
        if(adapter==null){
            adapter = new NightAuditListAdapter(this,model.getPNauStepsListDtoList());
            mListView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        CommonUtil.dismissProgressDialog(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_night_audit;
    }

    @Override
    public NightAuditPresenter newP() {
        return new NightAuditPresenter();
    }

    private int index;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_start_night_audit://开始审核按钮
                //todo 判断网络状态
                adapter.startAnim(true);
                index = 0;
                String fileName = ((NightAuditModel.PNauStepsListDtoListBean)adapter.getItem(index)).getFileName();
                if(!StringUtil.isEmpty(fileName)){
                    getP().startNightAudit(Constant.LAUGUAGE_FLAG_CHINESE,(fileName));
                }else{
                    adapter.setIcon(index,9);
                    loadSuccess(9);
                }
                break;
        }
    }

    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        LogUtil.showLog("error="+ StringUtil.errMsg(state));
        mListView.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListView.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            getP().asyncListData(Constant.LAUGUAGE_FLAG_CHINESE);
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
    }

    public void loadSuccess(int state){
        adapter.setIcon(index,state);
        index++;
        if(index < adapter.getCount()){
            getP().startNightAudit(Constant.LAUGUAGE_FLAG_CHINESE,((NightAuditModel.PNauStepsListDtoListBean)adapter.getItem(index)).getFileName());
        }
    }
}
